
# ========  配置：手动配置一下，debug =====
#DEBUG=-DCMAKE_BUILD_TYPE=Debug
DEBUG=-DCMAKE_BUILD_TYPE=Release
out_dir=$PWD/aout
echo -e "\e[7;36m ================== 开始编译 ======================\n 类型=$DEBUG  输出=$out_dir \e[0m"


# 1. 编译器选择
echo "--1.编译器选择: 
1) ubuntu:       ./b.sh clean(可选)
2) ubuntu 自动安装 ./b.sh install clean(可选)，自动安装，请使用  sudo
3)arm32: ./b.sh 32 clean(可选) 
4)arm64: ./b.sh 64 clean(可选)"

u_c=gcc
u_cpp=g++
arm32_c=arm-linux-gnueabihf-gcc
arm32_cpp=arm-linux-gnueabihf-g++
arm64_c=aarch64-linux-gnu-gcc
arm64_cpp=aarch64-linux-gnu-g++

CC=$u_c
CPP="$u_cpp"
CLEAN_BUILD=OFF

if [ "$1" = "32" ]; then
    CC=$arm32_c
    CPP=$arm32_cpp
    echo "选择 arm-32 编译"
    if [ "$2" = "clean" ];then
        CLEAN_BUILD=ON
    fi
elif [ "$1" = "64" ]; then
    CC=$arm64_c
    CPP=$arm64_cpp
    echo "选择 arm-64 编译"
    if [ "$2" = "clean" ];then
        CLEAN_BUILD=ON
    fi
elif [ "$1" = "install" ]; then
    output_dir=
    if [ "$2" = "clean" ];then
        CLEAN_BUILD=ON
    fi
    echo "默认 ubuntu 编译，并自动安装"
else
    echo "默认 unbuntu 编译"
    if [ "$1" = "clean" ];then
        CLEAN_BUILD=ON
    fi
fi

echo -e "\033[32m当前编译器信息 CC=$CC    CPP=$CPP  CLEAN_BUILD=$CLEAN_BUILD\033[0m"


# 2. 打包路径选择
OUTPUT=-DCMAKE_INSTALL_PREFIX=$out_dir
echo "--2.库文件打包路径 $output_dir"



# 3. 通用函数
# 函数1： 检查目录下面的 build
function check_build_exist
{
    echo "检查目录:$PWD/build"
    if [ -d "build" ]; then
        echo "目录 build 已经存在"
        if [ "$CLEAN_BUILD" = "ON" ];then
            echo "清除 build 缓存"
            rm ./build/*  -rf
        fi
    else
        mkdir build
    fi
}
# 函数2： 删除安装目录下的内容
function clean_dir
{
    dir=$1
    echo "清除目录数据 = $dir"
    if [ -d $dir ]; then
        num=`echo $dir | tr -cd "/" | wc -c`
        if [ $num -gt 1 ]; then 
            rm $dir/* -rf
            echo "num = $num 路径合法，删除 $dir 下的文件"
        fi
    fi
}

# 函数3： 统一编译函数
function run
{
    #参数1：编号 #参数2： 目录， #参数2：cmake 额外参数
    PROJ_NUM=$1
    PROJ_NAME=$2
    CMAKE_PARAMS=$3
    echo "======================== $PROJ_NUM 编译 $PROJ_NAME ================="
    clean_dir $out_dir/$PROJ_NAME
    cd $PROJ_NAME/
    check_build_exist
    cd build
    echo "cmake $OUTPUT/$PROJ_NAME $DEBUG -DCMAKE_C_COMPILER=/usr/bin/$CC -DCMAKE_CXX_COMPILER=/usr/bin/$CPP $CMAKE_PARAMS .."
    cmake $OUTPUT/$PROJ_NAME $DEBUG -DCMAKE_C_COMPILER=/usr/bin/$CC -DCMAKE_CXX_COMPILER=/usr/bin/$CPP $CMAKE_PARAMS ..
    make && make install
    cd ../../
}

# 通用编译选项：
CMAKE_PARAMS_DEFAULT="-DBUILD_STATIC_LIBS=OFF -DBUILD_SHARED_LIBS=ON"

# 编译 cserial-port
run 1 cserial-port "$CMAKE_PARAMS_DEFAULT -DCSERIALPORT_BUILD_EXAMPLES=OFF"

# # 编译 easySQLite: 
# echo -e "\033[33measySQLite 不编译 test 请在 CMakeLists.txt 中删除 add_subdirectory(test)  和 add_subdirectory(test/UnitTest++)\033[0m"
# run 2 easySQLite "$CMAKE_PARAMS_DEFAULT"

# # 编译 jsoncpp 
# run 3 jsoncpp "$CMAKE_PARAMS_DEFAULT -DJSONCPP_WITH_TESTS=OFF"


# # 编译 libhv 
# run 4 libhv "$CMAKE_PARAMS_DEFAULT -DBUILD_EXAMPLES=OFF -DWITH_OPENSSL=ON -DBUILD_STATIC=OFF"

# # 编译 paho.mqtt.c 
# echo -e "\033[33 zlog 请在 CMakeLists.txt 中删除 INCLUDE(CPack) \033[0m"
# run 5 paho.mqtt.c "$CMAKE_PARAMS_DEFAULT \
# -DPAHO_WITH_SSL=TRUE -DPAHO_BUILD_STATIC=FALSE -DPAHO_BUILD_SHARED=TRUE \
# -DPAHO_ENABLE_TESTING=FALSE -DPAHO_ENABLE_CPACK=FALSE -DPAHO_BUILD_DEB_PACKAGE=FALSE"

# # 编译 zlog 
# echo -e "\033[33 zlog 不自动安装（可能出现权限问题）请在 CMakeLists.txt 中删除 add_subdirectory(cpack) \033[0m"
# run 6 zlog "$CMAKE_PARAMS_DEFAULT -DUNIT_TEST=FALSE"

